package com.tiancheng.onecode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.onecode.entity.Role;
import com.tiancheng.onecode.entity.UserRole;
import com.tiancheng.onecode.mapper.RoleMapper;
import com.tiancheng.onecode.mapper.UserRoleMapper;
import com.tiancheng.onecode.model.response.admin.PlatformMerchantUserRoleQueryRes;
import com.tiancheng.onecode.service.IRoleService;
import com.tiancheng.onecode.service.IUserRoleService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: kellen
 * @Description:用户角色管理 service impl
 * @Date: create in 2024/12/9 18:22
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Override
    public void saveUserRole(Long platformMerchantUserId, List<Long> roleIds) {
        this.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getPlatformMerchantUserId,platformMerchantUserId).in(UserRole::getRoleId,roleIds));
        final List<UserRole> userRoles = new ArrayList<>();
        for (Long roleId : roleIds) {
            final UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setPlatformMerchantUserId(platformMerchantUserId);
            userRole.setCreatedDt(LocalDateTime.now());
            userRoles.add(userRole);
        }
        this.saveBatch(userRoles);
    }

    @Override
    public List<PlatformMerchantUserRoleQueryRes> queryByPmuIds(List<Long> pmuIds) {
        return baseMapper.queryByPmuIds(pmuIds);
    }
}
